import random
from lxml import etree


class CoreTrait(object):
	def __init__(self, *args, **kw):
		object.__init__(self)
		self.name = kw["name"]
		self.desc = kw["desc"]

class CoreTraits(object):
	def __init__(self, *args, **kw):
		object.__init__(self)
#
#		we're going to read trait definitions from
#		an xml file
#
		tree = etree.parse("resources/CoreTraits.xml")
		root = tree.getroot()
#
#		let's flag errors and report them at the end
#		save spamming the console
#
		no_name = False
		no_desc = False
#
#		set up a list where the trait definitions may reside
#		a dict too for fast lookups
#
		self.list = []
		self.lookup = {}
#
#		loop through the file
#
		for el in root:
#
#			get name and description
#			or None if they are missing
#
			name = el.get("name", None)
			desc = el.get("desc", None)
#
#			don't store malformed traits
#			(Unless the trait itself is "Malformed" of course)
#
			if name == None:
				no_name = True
			if desc == None:
				print "no desc for trait ", name
				no_desc = True
			if name == None or desc == None:
				continue
#
#			add well formed traits to the trait list
#
			ct = CoreTrait(name = name, desc = desc)
			self.list.append(ct)
			self.lookup[ct.name] = ct

#
#		I'm not usually this formal - must be in an odd mood
#
		if no_name:
			print "*** Warning: CoreTraits.xml contains traits lacking names"
		if no_desc:
			print "*** Warning: CoreTraits.xml contains "+\
				"traits lacking descriptions"
		if no_name or no_desc:
			print "*** Malformed traits have not been loaded"

	def find(self, name):
		return self.lookup.get(name, None)


core_traits = None

#
# the trait class does double duty - it has a random percentage
# for the generics, but just a name for the uniques
#
#
# I have plans to make the trait objects more complicated
# building in some of the game effects into the XML files
#
# for now though, let's not complicate it any further
#
class Trait(object):
	def __init__(self, *args, **kw):
		global core_traits

		object.__init__(self)
#
#		the trait name should always be there
#
		self.name = kw["name"]
#
#		default percent to 100 and random to false
#
		self.percent = kw.get("percent", 100)
		self.random = int(kw.get("random", False))
#
#		some sort of lookup from the traits xml file
#		is needed around here
#
		if core_traits == None:
			core_traits = CoreTraits()
		ct = core_traits.find(self.name)
#
#		we're likely to see traits that are not in the 
#		core traits. I'm going to call these "cosmetic"
#		show them in the trait list for role play purposes
#
#		I think there's three categories of trait
#		mechanistic, which affect game mechanics
#		fetish, which affect which customers are attracted
#		and cosmetic, which are just for decoration
#		maybe categorical for traits that mark a
#		girl as belonging to a certain category, like "Demon"
#		or "Robot"
#
#		Of course a trait can be mechanistic, fetishistic and
#		categorical all together. But cosmetic ones are only
#		ever cosmetic, by definition But I digress...
#
#		TL;DNR:	traits may legally be absent from the core traits
#		file. If so, we're done
#
		if ct == None:
			self.desc = None
			return
#
#		if we add more data to the core traits file
#		we can propagate them here
#
		self.desc = ct.desc

#
#	tests the trait's percentage for random girls
#
	def test(self):
		r = random.randint(0,100)
		truth = (r < int(self.percent))
		#print "{} < {} truth value = {}".format(
		#	r, self.percent, truth
		#)
		#if truth:
		#	print "trait passes"
		#print ""
		return truth

	def __str__(self):
		return self.name

	

